package com.shujia.wyh.day24;

import java.io.*;

/*
        把d:\\大数据.mp4复制到当前项目目录下的copy.mp4中
        比较不同的方式速度
 */
public class CopyMp4Demo {
    public static void main(String[] args) {
//        copy1(); //字节流一次读写一个字节 大概是 26分钟
//        copy2(); //字节流一次读写一个字节数组 大概是 1911 毫秒
//        copy3(); //字节缓冲流读写一个字节   大概是 10246 毫秒
        copy4();  //字节缓冲流读写一个字节数组 大概是 577 毫秒
    }

    public static void copy4() {
        //创建字节缓冲输入流对象
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            long start = System.currentTimeMillis();
            bis = new BufferedInputStream(new FileInputStream("E:\\录屏\\17day23 File类 1.mp4"));
            bos = new BufferedOutputStream(new FileOutputStream("copy4.mp4"));
            //一次读写一个字节数组
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, length);
            }

            long end = System.currentTimeMillis();
            System.out.println("视频复制完毕，总耗时" + (end - start) + "毫秒");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy3() {
        //创建字节缓冲输入流对象
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            long start = System.currentTimeMillis();
            bis = new BufferedInputStream(new FileInputStream("E:\\录屏\\17day23 File类 1.mp4"));
            bos = new BufferedOutputStream(new FileOutputStream("copy3.mp4"));
            //一次读写一个字节
            int b = 0;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }

            long end = System.currentTimeMillis();
            System.out.println("视频复制完毕，总耗时" + (end - start) + "毫秒");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy2() {
        //字节流一次读写一个字节
        //创建字节输入流对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            long start = System.currentTimeMillis();

            fis = new FileInputStream("E:\\录屏\\17day23 File类 1.mp4");
            fos = new FileOutputStream("copy2.mp4");
            //一次读写一个字节数组
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, length);
            }
            long end = System.currentTimeMillis();
            System.out.println("视频复制完毕，总耗时" + (end - start) + "毫秒");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void copy1() {
        //字节流一次读写一个字节
        //创建字节输入流对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            long start = System.currentTimeMillis();

            fis = new FileInputStream("E:\\录屏\\17day23 File类 1.mp4");
            fos = new FileOutputStream("copy1.mp4");
            //一次读写一个字节
            int b = 0;
            while ((b = fis.read()) != -1) {
                fos.write(b);
            }
            long end = System.currentTimeMillis();
            System.out.println("视频复制完毕，总耗时" + (end - start) + "毫秒");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
